﻿using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Themes.Resources;
using HomagGroup.BaseUI.Router;
using Proxy;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using static HomagChina.CCHCS.ProcessResourceInternal;
using static HomagChina.CCHCS.Utility;

namespace HomagChina.CCHCS.ViewModels
{
    public class TransHistoryViewModel : NotifyPropertyChangedBase
    {
        //private SearchInfoHandle Handle_Search;
        private ObservableCollection<TransHistoryModel> _List_TransHistoryModel;

        public ObservableCollection<TransHistoryModel> List_TransHistoryModel
        {
            get => _List_TransHistoryModel;
            set { _List_TransHistoryModel = value; OnPropertyChanged(); }
        }

        private bool _FilterAll;

        public bool FilterAll
        {
            get => _FilterAll;
            set { _FilterAll = value; OnPropertyChanged(); }
        }

        private bool _InFeed;

        public bool InFeed
        {
            get => _InFeed;
            set { _InFeed = value; OnPropertyChanged(); }
        }

        private bool _OutFeed;

        public bool OutFeed
        {
            get => _OutFeed;
            set { _OutFeed = value; OnPropertyChanged(); }
        }

        private double _Height = 75;

        public double Height
        {
            get => _Height;
            set { _Height = value; OnPropertyChanged(); }
        }

        private System.Windows.Media.Brush _TableBack;

        public System.Windows.Media.Brush TableBack
        {
            get => _TableBack;
            set { _TableBack = value; OnPropertyChanged(); }
        }

        private DateTime _ArrivalTime = DateTime.Now.AddMonths(-1);

        public DateTime ArrivalTime
        {
            get => _ArrivalTime;
            set { _ArrivalTime = value; Refresh(); OnPropertyChanged(); }
        }

        private DateTime _DepartureTime = DateTime.Now;

        public DateTime DepartureTime
        {
            get => _DepartureTime;
            set { _DepartureTime = value; Refresh(); OnPropertyChanged(); }
        }

        private string _Reference;

        public string Reference
        {
            get => _Reference;
            set { _Reference = value; Refresh(); OnPropertyChanged(); }
        }

        public RelayCommand HandleChangeFilterCommand { get; }
        public RelayCommand HandleRefreshCommand { get; }
        public RelayCommand UnLoadedCommand { get; }
        public RelayCommand LoadedCommand { get; }

        private static DispatcherTimer timer;

        private string messageType;

        public TransHistoryViewModel()
        {
            //Handle_Search = new SearchInfoHandle();
            HandleChangeFilterCommand = new RelayCommand(HandleChangeFilter);
            HandleRefreshCommand = new RelayCommand(HandleRefresh);
            LoadedCommand = new RelayCommand(ViewloadedCommand);
            UnLoadedCommand = new RelayCommand(ViewUnloadedCommand);
            timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(1500)
            };
        }

        /// <summary>
        ///
        /// </summary>
        private void ViewloadedCommand()
        {
            FilterAll = InFeed = OutFeed = true;
            messageType = "All";
            Refresh();
        }

        /// <summary>
        /// Unloaded
        ///
        /// </summary>
        private void ViewUnloadedCommand()
        {
            timer.Stop();
        }

        public void Refresh()
        {
            //timer.Tick += (sender, args) =>
            //{
            //    if (!RouterDictionary["7"].functionKeyBarItems[2].MenuItems[0].IsChecked)
            //        List_TransHistoryModel = Handle_Search.GetTransHistoryMation(messageType, ArrivalTime, DepartureTime, Reference);
            //};

            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                timer.Start();
            }
        }

        /// <summary>
        ///
        /// </summary>
        private void HandleChangeFilter()
        {
            FilterAll = (InFeed && OutFeed);
            if (FilterAll == false)
            {
                messageType = InFeed ? "InFeed" : "OutFeed";
            }
            else
            {
                messageType = "All";
            }
        }

        private void HandleRefresh()
        {
            InFeed = OutFeed = FilterAll;
            if (FilterAll == false)
            {
                messageType = InFeed ? "InFeed" : "OutFeed";
            }
            else
            {
                messageType = "All";
            }
        }

        /// <summary>
        ///
        /// </summary>

        public void HandleIsShowFilter()
        {
            var MainWindow = Application.Current.MainWindow;
            var Filter = (ContentControl)MainWindow.FindName("contentControl");
            var content = Filter.Content as FrameworkElement;
            var element = content.FindName("FilterElement") as FrameworkElement;
            var startBoard = new Storyboard();
            var animation = new DoubleAnimation();
            var currentStatus = NavigateServices.Instance.CurrentRouteInfo.functionKeyBarItems[0];
            if (currentStatus.Header as string == GetLocalText("Filter_off"))
            {
                animation = new DoubleAnimation()
                {
                    From = 0,
                    To = 75,
                    Duration = TimeSpan.FromSeconds(0.5)
                };

                currentStatus.Header = GetLocalText("Filter_on");
                currentStatus.Icon = GetIconBrush(Icon.ViewGrid, overlayIcon: OverlayIcon.Check);
            }
            else
            {
                animation = new DoubleAnimation()
                {
                    From = 75,
                    To = 0,
                    Duration = TimeSpan.FromSeconds(0.5)
                };

                currentStatus.Header = GetLocalText("Filter_off");
                currentStatus.Icon = GetIconBrush(Icon.ViewGrid, overlayIcon: OverlayIcon.Check, crossedOut: true);
            }
            ;
            startBoard.Children.Add(animation);
            Storyboard.SetTargetProperty(animation, new PropertyPath("Height"));
            Storyboard.SetTarget(animation, element);
            startBoard.Begin();
        }

        public void HandleIsFreeze()
        {
            var currentRouter = NavigateServices.Instance.CurrentRouteInfo;
            TransHistoryViewModel TransHistoryDataContext = (TransHistoryViewModel)currentRouter.userControl.DataContext;
            switch (currentRouter.functionKeyBarItems[2].MenuItems[0].IsChecked)
            {
                case true:
                    timer.Stop();
                    TransHistoryDataContext.TableBack = GetHexadecimalColor("#ffdd9c");
                    currentRouter.functionKeyBarItems[2].MenuItems[1].IsEnabled = false;
                    break;

                case false:
                    timer.Start();
                    TransHistoryDataContext.TableBack = null;
                    currentRouter.functionKeyBarItems[2].MenuItems[1].IsEnabled = true;
                    break;

                default:
                    break;
            }
        }

        public void HandleIsHistory()
        {
            var currentRouter = NavigateServices.Instance.CurrentRouteInfo;
            TransHistoryViewModel TransHistoryDataContext = (TransHistoryViewModel)currentRouter.userControl.DataContext;
            switch (currentRouter.functionKeyBarItems[2].MenuItems[1].IsChecked)
            {
                case true:
                    TransHistoryDataContext.TableBack = GetHexadecimalColor("#f79898");
                    currentRouter.functionKeyBarItems[2].MenuItems[0].IsEnabled = false;
                    break;

                case false:
                    TransHistoryDataContext.TableBack = null;
                    currentRouter.functionKeyBarItems[2].MenuItems[0].IsEnabled = true;
                    break;

                default:
                    break;
            }
        }
    }
}